package com.lyw.forum.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lyw.forum.pojo.dataobject.ArticleReply;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ArticleReplyMapper extends BaseMapper<ArticleReply> {

    /**
     * 根据帖子id查询所有回复
     * @param id 帖子id
     * @return 回复集合
     */
    @Select({
            "SELECT",
            "    u.id AS u_id,",
            "    u.avatar_url AS u_avatar_url,",
            "    u.nickname AS u_nickname,",
            "    ar.id,",
            "    ar.article_id,",
            "    ar.post_user_id,",
            "    ar.reply_id,",
            "    ar.reply_user_id,",
            "    ar.content,",
            "    ar.like_count,",
            "    ar.state,",
            "    ar.create_time,",
            "    ar.update_time",
            "FROM t_article_reply ar, t_user u",
            "WHERE ar.post_user_id = u.id",
            "    AND ar.delete_state = 0",
            "    AND ar.article_id = #{id}",
            "    order by ar.create_time desc"
    })
    @Results({
            // 用户对象字段映射 (使用完整路径)
            @Result(property = "user.id", column = "u_id"),
            @Result(property = "user.avatarUrl", column = "u_avatar_url"),
            @Result(property = "user.nickname", column = "u_nickname")
    })
    List<ArticleReply> selectByArticleId (Integer id);
}
